/*
* Copyright @2009-2012 www.happyelements.com, all rights reserved.
* Create date: 2012-7-30
* Piggy Dong
* 董文弈
* wenyi.dong@happyelements.com
*/
package com.happyelements.data
{
	import com.happyelements.isolib.IsoTileAnimation;
	import com.happyelements.isolib.IsoWorld;
	import com.happyelements.isolib.IsoTileObject;
	/**
	 * @author dongpiggyi
	 */
	public class DataSerializer
	{
		private static var _instance:DataSerializer;
		
		private var _serializedResult:String;
		
		public function DataSerializer(entrance:EntranceClass) 
		{
			if (!entrance)
			{
				throw new Error("Illigal instantiation of Singleton DataSerializer");
			}
		}
		
		public static function get instance():DataSerializer
		{
			_instance || (_instance = new DataSerializer(new EntranceClass()));
			
			return _instance;
		}
		
		public function calculateResult(dataCollection:Vector.<IsoTileObject>):String
		{
			_serializedResult = "";
			addLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			addLine("<size>" + IsoWorld.instance.numWorldGrid + "</size>");
			
			dataCollection.length > 0 ? addLine("<items>") : null;
			
			for each (var item:IsoTileObject in dataCollection) 
			{
				addLine("\t<item id=\"" + IsoTileAnimation(item).assetID + "\" col=\"" + item.columnIndex + "\" row=\"" + item.rowIndex + "\"/>"); 
			}
			
			dataCollection.length > 0 ? addLine("</items>") : null;
			
			return _serializedResult;
		}

		public function get serializedResult():String
		{
			return _serializedResult;
		}
		
		private function addLine(value:String):void
		{
			_serializedResult += value + "\n";
		}
	}
}

class EntranceClass
{
}
